﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using com.echonest.api.v4;
using jENIKVMApi;
using RequestEchoNest.Data;

namespace RequestEchoNest
{
    internal  static class ArtistAPIAsynchronousExtensions
    {
        public static IAsyncResult GetSimilarArtistsAsync(this  EchoNestAPI api,ArtistParams @params,AsyncCallback asyncCallback)
        {
            var asyncResult = new AsyncResult();
            ThreadPool.QueueUserWorkItem((state) =>
                                             {
                                                 List<Artist> artists = api.GetSimilarArtists(@params);
                                                 if (artists == null || artists.Count == 0)
                                                 {
                                                     asyncResult.AsyncState = null;
                                                     asyncCallback.Invoke(asyncResult);
                                                     return; 
                                                 }
                                                 asyncResult.AsyncState = artists;
                                                 asyncCallback.Invoke(asyncResult);
                                             }
                                             );
            return asyncResult;
        }
    }
}
